const int basesize = 1024;
const int argvnum = 64;
const int envnum = 64;
char *gargv[argvnum];
int gargc = 0; 
int lastcode = 0;
char *genv[envnum];

 // 全局的当前shell⼯作路径 
 char pwd[basesize];
 char pwdenv[basesize];

 // 全局变量与重定向有关 
 #define NoneRedir 0
 #define InputRedir 1
 #define OutputRedir 2
 #define AppRedir 3

 int redir = NoneRedir;
 char *filename = nullptr;

 // " "file.txt
 #define TrimSpace(pos) do{\
 while(isspace(*pos)){\
 pos++;\
 }\
 }while(0)

 string GetUserName()
 {
 string name = getenv("USER");
 return name.empty() ? "None" : name;
 }

 string GetHostName()
 {
 string hostname = getenv("HOSTNAME");
 return hostname.empty() ? "None" : hostname;
 }

 string GetPwd()
 {
 if(nullptr == getcwd(pwd, sizeof(pwd))) return "None";64 snprintf(pwdenv, sizeof(pwdenv),"PWD=%s", pwd);
 putenv(pwdenv); // PWD=XXX
 return pwd;

 //string pwd = getenv("PWD");
 //return pwd.empty() ? "None" : pwd;
 }

ring LastDir()
 {
 string curr = GetPwd();
 if(curr == "/" || curr == "None") return curr;
 // /home/whb/XXX
 size_t pos = curr.rfind("/");
 if(pos == std::string::npos) return curr;
 return curr.substr(pos+1);
 }

 string MakeCommandLine()
 {
 // [whb@bite-alicloud myshell]$ 
 char command_line[basesize];
 snprintf(command_line, basesize, "[%s@%s %s]# ",\
 GetUserName().c_str(), GetHostName().c_str(), LastDir().c_str());88 return command_line;
 }

 void PrintCommandLine() // 1. 命令⾏提⽰符 
 {
 printf("%s", MakeCommandLine().c_str());
 fflush(stdout);
 }

 bool GetCommandLine(char command_buffer[], int size) // 2. 获取⽤⼾命令 
 {
 // "ls -a -l -n"
 char *result = fgets(command_buffer, size, stdin);
 if(!result)
 {
 return false;
 }
 command_buffer[strlen(command_buffer)-1] = 0;
 if(strlen(command_buffer) == 0) return false;
 return true;
 }
